﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using VideoRentalShop.Model;

namespace VideoRentalShop.Controller.Data
{
    public class GenreDetailsDataSource : DataSourceBase<GenreDetails>
    {
        public GenreDetailsDataSource(MovieDbContext db) : base(db) { }


        public DbSet<GenreDetails> GetAll()
        {
            return _db.GenreDetails;
        }


        public override bool IsValid(GenreDetails item)
        {
            return _db.Entry(item).GetValidationResult().IsValid;
        }

        public override bool Add(GenreDetails item)
        {
            var entry = _db.Entry<GenreDetails>(item);
            var valid = entry.GetValidationResult().IsValid;
            if (valid)
            {
                entry.State = EntityState.Added;
                return Save();
            }

            return valid;
        }

        public override bool Edit(GenreDetails item)
        {
            var entry = _db.Entry<GenreDetails>(item);
            var valid = entry.GetValidationResult().IsValid;
            if (valid)
            {
                entry.State = EntityState.Modified;
                return Save();
            }

            return valid;
        }

        public override bool Remove(GenreDetails item)
        {
            var result = _db.GenreDetails.Find(item.Id);
            if (result != null)
            {
                _db.Entry(result).State = EntityState.Deleted;
                return Save();
            }

            return false;
        }
        
        public bool Remove(Movie item)
        {
            var list = item.Genres;
            if (item != null && list.Count > 0)
            {
                var test = list.Count;
                for (var i = 0; i < test; ++i)
                {
                    _db.Entry(list[0]).State = EntityState.Deleted;
                }
                return Save();
            }

            return false;
        }

        public bool Remove(Genre item)
        {
            var list = item.Details;
            if (item != null && list.Count > 0)
            {
                var test = list.Count;
                for (var i = 0; i < test; ++i)
                {
                    _db.Entry(list[0]).State = EntityState.Deleted;
                }
                return Save();
            }

            return false;
        }
    }
}
